<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input
            v-model="queryParams.deviceNo"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备状态" prop="situation">
          <el-select v-model="queryParams.situation" placeholder="请选择设备状态" clearable>
            <el-option
              v-for="dict in dict.type.device_situation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择设备类型" clearable>
            <el-option
              v-for="dict in dict.type.device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input
            v-model="queryParams.deptId"
            placeholder="请输入部门id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="绑定用户id" prop="customerId">
          <el-input
            v-model="queryParams.customerId"
            placeholder="请输入绑定用户id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="绑定用户名称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入绑定用户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备归属" prop="ownershipFlag">
          <el-select v-model="queryParams.ownershipFlag" placeholder="请选择设备归属" clearable>
            <el-option
              v-for="dict in dict.type.device_ownership_flag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备二维码" prop="qrCode">
          <el-input
            v-model="queryParams.qrCode"
            placeholder="请输入设备二维码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleBind()"
            v-hasPermi="['customer:customerDeviceBind:add']"
          >绑定
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange" >
        <el-table-column type="selection" width="55" align="center" :selectable="dataRowDisabled"/>
        <el-table-column label="设备编号" align="center" prop="deviceNo"/>
        <el-table-column label="设备状态" align="center" prop="situation">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.device_situation" :value="scope.row.situation"/>
          </template>
        </el-table-column>
        <el-table-column label="设备名称" align="center" prop="name"/>
        <el-table-column label="设备类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.device_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="设备描述" align="center" prop="remark"/>
        <el-table-column label="绑定用户名称" align="center" prop="customerName"/>
        <el-table-column label="设备归属" align="center" prop="ownershipFlag">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.device_ownership_flag" :value="scope.row.ownershipFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="handleBind(scope.row.id,scope.row.type)"
              v-hasPermi="['customer:customerDeviceBind:add']"
              :disabled="scope.row.customerId!=null"
            >绑定
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />


    </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from '@/api/device/device'
import {
  addCustomerDeviceBind,
  delCustomerDeviceBind,
  getCustomerDeviceBind,
  listCustomerDeviceBind,
  updateCustomerDeviceBind
} from '@/api/customer/customerDeviceBind'

export default {
  name: 'DeviceSelect',
  props: {
    customerId: {
      type: String,
      default: ''
    },
    popupVisible: {
      type: Boolean,
      default: false
    },
  },
  dicts: ['delete_flag', 'device_type', 'device_ownership_flag', 'device_situation', 'device_account_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      deviceTypes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      deviceList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNo: null,
        situation: null,
        name: null,
        type: null,
        deptId: null,
        customerId: null,
        customerName: null,
        ownershipFlag: null,
        qrCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.queryParams.notCustomerId=this.customerId
    this.form.customerId=this.customerId
    this.getList()
  },
  watch:{
    popupVisible(newV,old){
      if(newV){
        this.getList()
      }
    }
  },
  methods: {
    dataRowDisabled(row,rowIndex){
      return row.customerId==null;
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        deviceId: null,
        bindTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deviceType: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.deviceTypes = selection.map(item => item.type)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleBind(id,type) {
      let ids,deviceTypes
      if(id){
        ids=id.split(',')
        deviceTypes = type.split(',')
      }else{
        ids = this.ids
        deviceTypes = this.deviceTypes
      }

      if(!ids) {
        this.$modal.msgSuccess('请先勾选需要绑定的设备')
        return
      }
      const params={}
      params.deviceIds=ids
      params.deviceTypes=deviceTypes
      params.customerId=this.form.customerId

      addCustomerDeviceBind(params).then(response=>{
        this.$modal.msgSuccess("修改成功");
        this.handleQuery()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？').then(function() {
        return delDevice(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
